<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
 */

namespace App\Controller;


abstract class Base {

    /**
     * @\Hyperf\Di\Annotation\Inject
     * @var \Psr\Container\ContainerInterface
     */
    protected $di;

    /**
     * @\Hyperf\Di\Annotation\Inject
     * @var \Hyperf\HttpServer\Contract\RequestInterface
     */
    protected $request;

    /**
     * 获取参数并去除两端空格
     * 所有参数禁止数组形式传值
     * @param string $key
     * @return mixed
     */
    protected function get(string $key) {
        return $this->paramCheck($this->request->query($key));
    }

    /**
     * 获取参数并去除两端空格
     * 所有参数禁止数组形式传值
     * @param string $key
     * @return mixed
     */
    protected function body(string $key) {
        return $this->paramCheck($this->request->post($key));
    }

    /**
     * 参数格式转化，去除两端空格
     * @param $param
     * @return string|null
     */
    private function paramCheck($param) {
        if (is_array($param)) {
            return null;
        } else {
            return is_string($param) ? trim($param) : $param;
        }
    }

    /**
     * @param string $id 验证器类名
     * @param string $fun 方法名称
     */
    protected function validate(string $id, string $fun) {
        $validate = $this->di->get($id);
        $validate->check($validate->$fun());
    }
}
